package com.msconfig.mvp.view.fragment;

/*
 * author: caojunhao
 * date  : 2020/5/13
 * desc  :
 */

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.msconfig.bean.dialogBean.FourParameterBean;
import com.msconfig.event.ClickFourParameterDeterminerEvent;
import com.msconfig.event.NullEvent;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;
import com.msconfig.utils.SpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class SetupFourParameterFragment extends BaseMvpFragment implements View.OnClickListener{
    private static SetupFourParameterFragment fragment;
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private ImageView ivSwitchIsUseOn;
    private ImageView ivSwitchNoneUseOff;
    private ScrollView svContent;
    private EditText etXTranslation;
    private EditText etYTranslation;
    private EditText etRotate;
    private EditText etScale;
    private Button btnDetermine;
    private boolean isuse;
    private static final String ISUSEFOURPARAMETER ="ISUSEFOURPARAMETER";
    private FourParameterBean fourParameterBean;

    private static final String ISUSEFOURPARAMETERS="isusefourparameters";
    private static final String XTRANSLATION="xtranslation";
    private static final String YTRANSLATION="ytranslation";
    private static final String ROTATE="rotate";
    private static final String SCALE="scale";


    public static SetupFourParameterFragment newInstance(FourParameterBean fourParameterBean) {
        Bundle args = new Bundle();
        fragment = new SetupFourParameterFragment();

        if(fourParameterBean==null){
            fragment.setArguments(null);
        }else{
            args.putBoolean(ISUSEFOURPARAMETERS,fourParameterBean.getIsUse());
            if(fourParameterBean.getIsUse()){
                args.putString(XTRANSLATION,fourParameterBean.getxTranslation());
                args.putString(YTRANSLATION,fourParameterBean.getyTranslation());
                args.putString(ROTATE,fourParameterBean.getRotate());
                args.putString(SCALE,fourParameterBean.getScale());
            }else{
                args.putString(XTRANSLATION,"");
                args.putString(YTRANSLATION,"");
                args.putString(ROTATE,"");
                args.putString(SCALE,"");
            }
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            backTo();
        } else if(v==ivSwitchIsUseOn){
            onClickSwitchOn();
        }else if(v==ivSwitchNoneUseOff){
            onClickSwitchOff();
        }else if(v==btnDetermine){
            onClickDetermine();
            backTo();
        }
    }

    private void onClickSwitchOff() {
        ivSwitchNoneUseOff.setVisibility(View.GONE);
        ivSwitchIsUseOn.setVisibility(View.VISIBLE);
        svContent.setVisibility(View.VISIBLE);
    }

    private void onClickSwitchOn() {
        ivSwitchNoneUseOff.setVisibility(View.VISIBLE);
        ivSwitchIsUseOn.setVisibility(View.GONE);
        svContent.setVisibility(View.GONE);
    }

    private void onClickDetermine() {
        fourParameterBean =new FourParameterBean();
        if(ivSwitchIsUseOn.getVisibility()== View.VISIBLE){
            isuse=true;
        }else{
            isuse=false;
        }
        SpUtils.obtain().save(ISUSEFOURPARAMETER,isuse);
        fourParameterBean.setIsUse(isuse);
        if(isuse){
            fourParameterBean.setxTranslation(etXTranslation.getText().toString().trim());
            fourParameterBean.setyTranslation(etYTranslation.getText().toString().trim());
            fourParameterBean.setRotate(etRotate.getText().toString().trim());
            fourParameterBean.setScale(etScale.getText().toString().trim());
        }
        EventBus.getDefault().post(new ClickFourParameterDeterminerEvent(fourParameterBean));
    }


    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_setup_four_parameter;
    }


    @Override
    protected void initView(View view) {
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        ivSwitchIsUseOn = (ImageView)view.findViewById( R.id.iv_switch_is_use_on );
        ivSwitchNoneUseOff = (ImageView)view.findViewById( R.id.iv_switch_none_use_off );
        svContent = (ScrollView)view.findViewById( R.id.sv_content );
        etXTranslation = (EditText)view.findViewById( R.id.et_x_translation );
        etYTranslation = (EditText)view.findViewById( R.id.et_y_translation );
        etRotate = (EditText)view.findViewById( R.id.et_rotate );
        etScale = (EditText)view.findViewById( R.id.et_scale );
        btnDetermine = (Button)view.findViewById( R.id.btn_determine);

    }

    @Override
    protected void initListener(View view) {
        ivCommonBack.setOnClickListener(this);
        btnDetermine.setOnClickListener( this );
        ivSwitchIsUseOn.setOnClickListener( this );
        ivSwitchNoneUseOff.setOnClickListener( this );
    }

    @Override
    protected void initData() {
        tvCommonTitle.setText(R.string.title_four_parameter);
        if(SpUtils.obtain().getBoolean(ISUSEFOURPARAMETER)){
            ivSwitchNoneUseOff.setVisibility(View.GONE);
            ivSwitchIsUseOn.setVisibility(View.VISIBLE);
            svContent.setVisibility(View.VISIBLE);
            SpUtils.obtain().save(ISUSEFOURPARAMETER, false);
        }

        Bundle bundle = fragment.getArguments();
        if(bundle!=null){
            if(bundle.getBoolean(ISUSEFOURPARAMETERS)){
                onClickSwitchOff();
            }else{
                onClickSwitchOn();
            }
            etXTranslation.setText(bundle.getString(XTRANSLATION));
            etYTranslation.setText(bundle.getString(YTRANSLATION));
            etRotate.setText(bundle.getString(ROTATE));
            etScale.setText(bundle.getString(SCALE));

        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showAnalysisHostDataSuccess(NullEvent event) {
    }


}
